﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Http.Routing;
using AutoMapper;
using Boggle;
using Web.Models;

namespace Web.Controllers.Api
{
    public class BoggleController : ApiController
    {
        private readonly IBoggleSolver _boggleSolver;

        public BoggleController(IBoggleSolver boggleSolver)
        {
            _boggleSolver = boggleSolver;
        }

        public BoggleSolutionViewModel Solve(BoggleBoardViewModel boggleBoardViewModel)
        {
            BoggleBoard boggleBoard = new BoggleBoard(boggleBoardViewModel.Height, boggleBoardViewModel.Width, boggleBoardViewModel.Letters);
            IEnumerable<BoggleTraversal> wordTraversals = _boggleSolver.Solve(boggleBoard);
            BoggleSolutionViewModel boggleSolutionViewModel = new BoggleSolutionViewModel
            {
                Board = boggleBoardViewModel,
                WordTraversals = wordTraversals.Select(Mapper.Map<BoggleTraversal, BoggleTraversalViewModel>).OrderBy(wordTraversal => wordTraversal.Word)
            };
            return boggleSolutionViewModel;
        }
    }
}